package common;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipFileSearch {
	
	
	
	public void zipFileRead(String file) {  
        try {  
            // 获得zip信息  
            ZipFile zipFile = new ZipFile(file);  
            @SuppressWarnings("unchecked")  
            Enumeration<ZipEntry> enu = (Enumeration<ZipEntry>) zipFile.entries();  
            while (enu.hasMoreElements()) {  
                ZipEntry zipElement = (ZipEntry) enu.nextElement();  
               // InputStream read = zipFile.getInputStream(zipElement);  
                String fileName = zipElement.getName();  
                if (fileName != null && fileName.indexOf(".") != -1) {// 是否为文件  
                  //  unZipFile(zipElement, read, saveRootDirectory);  
                    System.out.println("-"+fileName.substring(fileName.lastIndexOf("/")+1));
                }  else {
                	System.out.println("+"+zipElement.getName());
                }
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    } 
	
	
	public void  zipNioRead(String file) {
        try {
			java.nio.file.FileSystem fs = FileSystems.newFileSystem(Paths.get(file), null);	
			
			Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<Path>() {			
				@Override
				public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException  {
					System.out.println(file);
					return super.visitFile(file, attrs);
				}
				
			});
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	
	public static void main(String[] args) {
		ZipFileSearch  zipFileSearch = new ZipFileSearch();
		zipFileSearch.zipFileRead("D:\\temp\\qdm.zip");
		//zipFileSearch.zipNioRead("D:\\temp\\qdm.zip");
	}

}
